var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'opbal_mis_new_sum.html', controller: ExpenseMisCtrl}).
     otherwise({redirectTo: '/'});
}]);


function ExpenseMisCtrl($scope, $http,$window) 
   {
    $scope.currentPage = 0;
    $scope.pageSize = 100;
 	$scope.Math = window.Math;
	$scope.custarr = [];
	$scope.search2 = {};	
	var search1 = '';
	var d = new Date();					
	$scope.fromdt = d.getFullYear()+"-"+(parseInt(d.getMonth())+1)+"-"+d.getDate();
	$scope.todt = d.getFullYear()+"-"+(parseInt(d.getMonth())+1)+"-"+d.getDate();

    $scope.numberOfPages=function()
    {
        return Math.ceil($scope.data.length / $scope.pageSize);                
    };
 
	$http.get('opbal_mis_data_new_sum.php?q=2').success(function(data) {
		for(var i=0;i<data.length;i++){				
				$scope.custarr.push(data[i]);			
			}
			$scope.custarr.sort();	
		$scope.search1 =	$scope.custarr[0];
		var sql='opbal_mis_data_new_sum.php?q=1&selc='+$scope.search1+'&from='+$scope.fromdt+'&to='+$scope.todt;
		$http.get(sql).success(function(data) {		
			$scope.runningmis = data;   
			$scope.runningmis2 = data;   
		});	
    });			
	
	/*$scope.getdata = function() 
	{
		var url = 'opbal_mis_data_new.php?q=1&selc=all&from='+$scope.fromdt+'&to='+$scope.todt;
		$http.get(url).success(function(data) {				
			$scope.runningmis = data;   		
		});	
	}*/
	
	$scope.dfilter = function(from,to,cust) 
	{				
		/*//alert(from+":"+to);
		markers=$scope.runningmis2;
		var retMarkers = [];		
		var j=parseInt(0);
		
		if(!from)
			from='0000-00-00';
		
		if(!to)
		{
			var d = new Date();					
			var d2 = d.getFullYear()+"-"+(parseInt(d.getMonth())+1)+"-"+d.getDate();			
			to=d2;
		}
		
        for(var i = 0, len = markers.length; i < len; i++) 
		{
			var singleMarker = markers[i];
			if(singleMarker.rqdate >= from && singleMarker.rqdate <= to)
			{						
				retMarkers[j]=singleMarker;				
				j++;				    
			}
		}		
		$scope.runningmis = retMarkers;
		$scope.runningmis.length = j;	*/	
		
		if(from && to){
			if(!cust)
				cust='';
			var url = 'opbal_mis_data_new_sum.php?q=1&selc='+cust+'&from='+from+'&to='+to;
			
			
			$('button[data-loading-text]').button('loading');
			setTimeout(function () {
			   $http.get(url).success(function(data) {
					$scope.runningmis = data;   						
					$('button[data-loading-text]').button('reset');
				});
			}, 2000);						
		}
		else
			alert("Please enter both: a from date and to date");
	}

	 $scope.total_running = function() {
        var total = 0;
		var exist = false;
        angular.forEach($scope.runningmis, function(item) {         	
			if(item.customer == $scope.search2.customer || $scope.search2.customer == null || $scope.search2.customer == ''){
				exist =  true;
				total += +item.runningtotal;
			}			
        })
        return total;
	} 
	 
	$scope.cancel = function() 
	{		
		//$scope.runningmis=$scope.runningmis2;
		$scope.activePath = null;
		$scope.activePath = $location.path('opbal_mis_new_sum.php');
	}
    
}
	
	  
	    
	
//We already have a limitTo filter built-in to angular,
//let's make a startFrom filter
app.filter('startFrom', function() {
    return function(input, start) {
        start = +start; //parse to int
        return input.slice(start);
    };
});

app.filter('exact', function(){
  return function(items, match){
    var matching = [], matches, falsely = true;
	angular.forEach(match, function(value, key){
      falsely = falsely && !value;
    });
    if(falsely){
      return items;
    }
    angular.forEach(items, function(item){ // e.g. { title: "ball" }
      matches = true;
      angular.forEach(match, function(value, key){ // e.g. 'all', 'title'
        if(!!value) // do not compare if value is empty
          matches = matches && (item[key] === value);          
      });
      if(matches)
        matching.push(item);        
    });
    return matching;
  }
});

